<template>
  <div class="clipboardTarget" :copy-content="copyText" @click="handleUserPhoneInfo">
	  <slot name="deep-slot"></slot>
	</div>
</template>
<script>
	import MobileDetect from '../js/mobile-detect';
	import Clipboard from 'clipboard';
  export default {
		name: 'deeper-link',
		props: {
			activityChannel: '',
			copyText: '',
		},
    data() {
      return {
        deviceType: '',
        version: '',
        ip: ''
      }
    },
    methods: {
      getDeviceType() {
		let device_type = navigator.userAgent;
        let md = new MobileDetect(device_type); // 初始化mobile-detect
        let os = md.os(); // 获取系统
        let model = "";
        if (os === "iOS") { // ios系统的处理
          os = md.os() + md.version("iPhone");
          model = md.mobile();
        } else if (os === "AndroidOS") { // Android系统的处理
          os = md.os() + md.version("Android");
          let sss = device_type.split(";");
					for (let i = 0; i < sss.length; i++) {
						if (sss[i].indexOf("Build/")) {
							model = sss[i].substring(0, sss[i].indexOf("Build/"));
						}
					}
				}
				this.deviceType = model;
      },
      getOSVersion() {
        var OSVision = '1.0';
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; // Android
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);		// ios
        if (isAndroid) {
          OSVision = navigator.userAgent.split(';')[1].match(/\d+\.\d+/g)[0];
        }
        if (isIOS) {
          OSVision = navigator.userAgent.split(';')[1].match(/(\d+)_(\d+)_?(\d+)?/)[0];
        }
        this.version = OSVision;
      },

      async handleUserPhoneInfo() {	
				let w = window.screen.width > 600 ? window.screen.width / window.devicePixelRatio : window.screen.width;
				let h = window.screen.width > 600 ? window.screen.height / window.devicePixelRatio : window.screen.height;
        let phoneAttributes = {
          screenSize: `${w}/${h}`,
          deviceType: this.deviceType,
					version: this.version.replace(/_/ig, '.'),
					dpr: window.devicePixelRatio,
				}
				
        let params = {
          activityChannel: this.activityChannel, // 活动编号 渠道
          phoneAttributes: JSON.stringify(phoneAttributes)
        }
        // console.log('params', params);
        let res = await this.$http.post(`/platform-user/v1/pb/user-phone-attributes/action/submit`, params);
      }
	},
    mounted() {
		// 复制必须通过点击触发
		let clipboard = new Clipboard('.clipboardTarget', {
			text: function (trigger) {
				return trigger.getAttribute('copy-content')
			}
		});
		clipboard.on('success', e => {
			// alert('copy success');
		});

		clipboard.on('error', e => {
			// alert('copy error');
		});
		this.getDeviceType();
		this.getOSVersion();
		this.getIPs((ip) => { 
			this.ip = ip;
			// 有的机型不能拿到ip
			// this.handleUserPhoneInfo();
		});
    },
  }
</script>